package tech.abing.spider.task.pageprocessor;

import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.util.DateUtils;
import tech.abing.spider.util.StringUtils;

public class Five8Processor1 implements PageProcessor {
	static Pattern normalContain = Pattern.compile("58");
	
	@Override
	public PageScript getJavaScript() {
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		return normalContain;
	}

	@Override
	public void process(OkPage page, StartContext context,
			List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
	
		Document doc = Jsoup.parse(page.getContent());
		
		Elements infoEles = doc.select("#infolist > dl");
		
		Elements eles = doc.select("a.t");
		
		System.out.println("数量：" + eles.size());
		
		String maxPubDate = StringUtils.toString(page.getRequest().getParamsByName("maxPubDate"));
		
		for(Element ele : infoEles){
			
			String url = ele.select("dt > a").attr("href");
			
			String pubDate = ele.select("dd.w68").text();
			
			if(pubDate.indexOf("-") > -1){//需要判断抓取时间
				if(StringUtils.isNotBlank(maxPubDate)){
					
					Date date1 = DateUtils.parseDate("2015-"+pubDate, new String[]{"yyyy-MM-dd"});
					Date date2 = DateUtils.parseDate(maxPubDate, new String[]{"yyyy-MM-dd"});
					
					if(date1.before(date2)){
						return;
					}
					
				}
			}
			
			
			PageRequest req = context.createPageRequest(url, Five8Processor2.class, 1);
			req.putParams("category", StringUtils.toString(page.getRequest().getParamsByName("category")));
			queue.add(req);
			
		}
		
		//下一页
		Element nextEle = doc.select("a.next").first();
		if(nextEle != null){
			
			String nextUrl = nextEle.attr("href");
			PageRequest req = context.createPageRequest("http://sz.58.com"+nextUrl, Five8Processor1.class, 0);
			
			req.putParams("category", StringUtils.toString(page.getRequest().getParamsByName("category")));
			
			queue.add(req);
			
		}
		
	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}

}
